package so.sao.code.service.web.isv;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import so.sao.auth.autoreg.annotation.IsvResource;
import so.sao.code.dto.request.TerminalDownloadRequest;
import so.sao.code.dto.response.AvailableNum;
import so.sao.code.dto.response.TagAvailableTypeDto;
import so.sao.code.dto.response.TerminalDownloadResponse;
import so.sao.code.service.service.RangeActivationService;
import so.sao.code.service.service.TagBatchService;
import so.sao.code.service.service.TerminalDownloadResultService;
import so.sao.sso.bind.CurrentUser;
import so.sao.sso.feign.dto.IsvUserDTO;

@Api(tags = "terminalDownload", description = "工控机下载")
@RestController
@RequestMapping("isv/terminalDownload")
public class TerminalDownloadController {

	@Autowired
	public TagBatchService tagBatchService;
	@Autowired
	RangeActivationService rangeActivationService;
	@Autowired
	TerminalDownloadResultService terminalDownloadResultService;

	@ApiOperation(value = "工控机可下载码类型查询", notes = "工控机可下载码类型查询")
	@IsvResource(perm = "terminalDownload.getType", name = "工控机可下载码类型查询")
	@PostMapping(value = "/company/{code}/availableType")
	public ResponseEntity<List<TagAvailableTypeDto>> getAvailableType(@CurrentUser IsvUserDTO dto,
			@PathVariable("code") String code, @RequestParam(value = "terminalId", required = true) String terminalId) {
		return new ResponseEntity<List<TagAvailableTypeDto>>(
				tagBatchService.getAvailableTypeByTerminalId(Long.parseLong(terminalId), dto.getCompanyId()),
				HttpStatus.OK);
	}

	@ApiOperation(value = "工控机下载码", notes = "工控机下载码")
	@IsvResource(perm = "terminalDownload.downTag", name = "工控机下载码")
	@PostMapping(value = "/company/{code}/downloadByTerminal")
	public ResponseEntity<TerminalDownloadResponse> downTag(@CurrentUser IsvUserDTO dto,
			@Valid @RequestBody TerminalDownloadRequest terminalDownloadRequest, @PathVariable("code") String code,
			@RequestParam(value = "terminalId", required = true) String terminalId) throws Exception {
		TerminalDownloadResponse result = null;
		if (terminalDownloadRequest.getDownloadNum() <= 10000) {
			result = tagBatchService.downloadByTerminal(terminalDownloadRequest, Long.parseLong(terminalId),
					dto.getCompanyId());
		} else {
			result = new TerminalDownloadResponse();
			result.setCurrentId(0L);
			result.setResultMsg("请求码量过大");
		}
		return new ResponseEntity<TerminalDownloadResponse>(result, HttpStatus.OK);
	}

	@ApiOperation(value = "工控机下载码后更新状态", notes = "工控机下载码后更新状态")
	@IsvResource(perm = "terminalDownload.downloadStatus", name = "工控机下载码后更新状态")
	@PostMapping(value = "/company/{code}/downloadStatus")
	public ResponseEntity downloadStatus(@CurrentUser IsvUserDTO dto,
			@RequestParam(value = "successID", required = true) Long successID, @PathVariable("code") String code,
			@RequestParam(value = "terminalId", required = true) String terminalId) throws Exception {
		tagBatchService.updateTerminalDownload(successID);
		return new ResponseEntity(HttpStatus.OK);
	}

	@ApiOperation(value = "工控机可下载数量", notes = "工控机可下载数量")
	@IsvResource(perm = "terminalDownload.getAvailableNum", name = "工控机可下载数量")
	@PostMapping(value = "/company/{code}/downloadAvailableNum")
	public ResponseEntity<AvailableNum> getAvailableNum(@CurrentUser IsvUserDTO dto, @PathVariable("code") String code,
			@RequestParam(value = "terminalId", required = true) String terminalId,
			@RequestParam(value = "tagType", required = true) Integer tagType) throws Exception {
		return new ResponseEntity(tagBatchService.getAvailableNum(terminalId, tagType, dto.getCompanyId()), HttpStatus.OK);
	}
}
